package com.xtz.controller;

import cn.hutool.core.lang.Snowflake;
import com.xtz.domain.User;
import com.xtz.dto.UserDto;
import com.xtz.service.LoginService;
import com.xtz.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.math.BigInteger;
import java.security.SecureRandom;
import java.util.Random;
import java.util.concurrent.TimeUnit;

/**
 * date: 2022/5/16 18:58
 *
 * @author xietianze
 */
@RestController
@RequestMapping("user")
public class LoginController {
    @Autowired
    private LoginService loginService;
    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    @PostMapping("/sendMsg")
    public Result sendMsg(@RequestBody UserDto dto){
        String phone = dto.getPhone();
        Random random = new SecureRandom();
        StringBuilder stringBuilder = new StringBuilder();
        for (int i = 0; i < 4; i++) {
            int num = random.nextInt(9);
            if (num%2==0) {
                stringBuilder.append(num);
            } else {
                stringBuilder.append((char) (65 + random.nextInt(26)));
            }
        }
        //验证码
        String validateCode = stringBuilder.toString();
        ValueOperations forValue = redisTemplate.opsForValue();
        forValue.set(phone, validateCode);
        redisTemplate.expire(phone,60, TimeUnit.SECONDS);


        return new Result(1,null,"手机验证码发送成功",null);
    }

    @PostMapping("/login")
    public Result login(@RequestBody UserDto dto){


        String phone = dto.getPhone();
        //redis中的code
        // String rediscode = (String) redisTemplate.boundValueOps(phone).get();

        //传入的code
        String code = dto.getCode();


        //redis中的验证码
        ValueOperations<String, String> forValue = redisTemplate.opsForValue();
        String validateCodeInRedis = forValue.get(phone);


            User user = new User();
            String idStr = new Snowflake().nextIdStr();
            user.setId(idStr);
            // user.setToken(token);
            user.setPhone(dto.getPhone());
            user.setStatus(1);
        loginService.addUser(user);
        //校验
        if (code!=null && validateCodeInRedis!=null && code.equals(validateCodeInRedis)) {
//4.生成token
            String tokenString = System.currentTimeMillis() + code;
            String token = DigestUtils.md5DigestAsHex(tokenString.getBytes());

            //将token记录到redis
            redisTemplate.opsForValue().set(token, code,259200,TimeUnit.SECONDS);

            return new Result(1,null,user,null);
        }else {
            return new Result(0, null, user, null);
        }
        //     //4.生成token
        //     String tokenString = System.currentTimeMillis() + code.toString();
        //     String token = DigestUtils.md5DigestAsHex(tokenString.getBytes());
        //
        //     //将token记录到redis
        //     redisTemplate.opsForValue().set(token, String.valueOf(code),259200,TimeUnit.SECONDS);
        //
        //     User user = new User();
        //     user.setToken(token);
        //     user.setPhone(dto.getPhone());
        //
        // boolean equals = rediscode==code;
        // if (equals==true){
        //     return new Result(1,null,user,1);
        // }else {
        //     return new Result(0,null,user,1);
        // }






    }

    @PostMapping("/loginout")
    public Result LoginOut(HttpServletRequest request){
        String token = request.getHeader("Authorization");

        redisTemplate.delete(token);
        return new Result(1,null,"退出成功",null);
    }
}
